package com.randb.easyhttp.model;

import io.reactivex.Observable;

/**
 * User: RandBII
 * Date: 2020/5/8
 * Description:
 */
public class Optional<T> {

    private Observable<T> observable;

    public Optional(Observable<T> obs) {
        this.observable = obs;
    }


    public static <T> Optional<T> of(T value) {
        if (value == null) {
            throw new NullPointerException();
        } else {
            return new Optional<>(Observable.just(value));
        }
    }


    public T get() {
        return observable.blockingSingle();
    }

    public T orElse(T defaultValue) {
        return observable.defaultIfEmpty(defaultValue).blockingSingle();
    }

    public static <T> Optional<T> ofNullable(T value) {
        if (value == null) {
            return new Optional<>(Observable.<T>empty());
        } else {
            return new Optional<>(Observable.just(value));
        }

    }

}
